/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.uac.dao.IPermissionManageDao;
import com.eelpo.cmmp.uac.dao.IUserManageDao;
import com.eelpo.cmmp.uac.dao.entity.TAction;
import com.eelpo.cmmp.uac.dao.entity.TEntity;
import com.eelpo.cmmp.uac.dao.entity.TPermission;
import com.eelpo.cmmp.uac.dao.entity.TUser;
import com.eelpo.cmmp.uac.dao.vo.Permission;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IPermissionManageService;
import com.eelpo.common.util.BlankUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 
 * 权限管理服务层具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Service
public class PermissionManageService implements IPermissionManageService {

	@Resource
	protected IUserManageDao userManageDao;

	@Resource
	protected IPermissionManageDao permissionManageDao;

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IPermissionManageService#addPermission(com
	 *      .eelpo.cmmp.uac.dao.vo.Permission)
	 */
	@Override
	public String addPermission(Permission permission) throws ServiceException {

		// 检查权限是否已经存在
		if (permissionManageDao.checkExist(permission.getEntity().getId(), permission.getAction().getId())) {
			throw new ServiceException("权限已经存在！");
		}

		return permissionManageDao.addPermission(vo2po(permission));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.services.IPermissionManageService#getPermissions(java.lang.String)
	 */
	@Override
	public List<Permission> getPermissions(String userId) throws ServiceException {

		TUser user = userManageDao.getUserById(userId);

		if (BlankUtils.isBlank(user)) {
			throw new ServiceException("用户不存在！");
		}

		if (user.getSuperManager()) {
			return po2voRef(permissionManageDao.getPermissions());
		} else {
			return po2voRef(permissionManageDao.getPermissions(userId));
		}
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param permission
	 *            权限值对象
	 * @return 返回权限实体对象
	 */
	public static TPermission vo2po(Permission permission) {

		TPermission tpermission = new TPermission();

		BeanUtils.copyProperties(permission, tpermission, new String[] { "status", "entity", "action" });

		tpermission.setStatus(Status.valueOf(permission.getStatus()));

		if (!BlankUtils.isBlank(permission.getEntity()))
			tpermission.setEntity(new TEntity(permission.getEntity().getId()));

		if (!BlankUtils.isBlank(permission.getAction()))
			tpermission.setAction(new TAction(permission.getAction().getId()));

		return tpermission;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tpermission
	 *            权限实体对象
	 * @return 返回权限值对象
	 */
	public static Permission po2vo(TPermission tpermission) {

		Permission permission = new Permission();

		BeanUtils.copyProperties(tpermission, permission, new String[] { "status", "entity", "action" });

		permission.setStatus(Status.valueOf(tpermission.getStatus()));

		return permission;
	}

	/**
	 * 包含任何关联对象的转换
	 * 
	 * @param tpermission
	 *            权限实体对象
	 * @return 返回权限值对象
	 */
	public Permission po2voRef(TPermission tpermission, Permission permission) {

		permission.setAction(ActionManageService.po2vo(tpermission.getAction()));
		permission.setEntity(EntityManageService.po2vo(tpermission.getEntity()));

		return permission;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tpermissions
	 *            权限实体对象列表
	 * @return 返回权限值对象列表
	 */
	public static List<Permission> po2vo(List<TPermission> tpermissions) {
		List<Permission> permissions = new ArrayList<Permission>();
		for (TPermission tpermission : tpermissions) {
			permissions.add(po2vo(tpermission));
		}
		return permissions;
	}

	/**
	 * 含任何关联对象的转换
	 * 
	 * @param tpermissions
	 *            权限实体对象列表
	 * @return 返回权限值对象列表
	 */
	public List<Permission> po2voRef(List<TPermission> tpermissions) {
		List<Permission> permissions = new ArrayList<Permission>();
		for (TPermission tpermission : tpermissions) {
			Permission permission = po2vo(tpermission);
			permissions.add(po2voRef(tpermission, permission));
		}
		return permissions;
	}

}
